package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.GoodsAttrValuesMapper;
import com.ktmall.ktmalluijava.pojo.GoodsAttrValues;
import com.ktmall.ktmalluijava.service.GoodsAttrValuesService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/19 上午8:36
 */
@Service
public class GoodsAttrValuesServiceImpl implements GoodsAttrValuesService {

    @Resource
    private GoodsAttrValuesMapper mapper;
    /**
     * 获取分页对象
     *
     * @param goodsId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<GoodsAttrValues> getPage(Integer goodsId, Integer pageNo, Integer pageSize) {
        Page<GoodsAttrValues> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(mapper.count(goodsId));
        page.setPageNo(pageNo);
        int offset = (page.getPageNo()-1)*pageSize;
        page.setData(mapper.select(goodsId,offset,page.getPageSize()));
        return page;
    }

    /**
     * 添加商品关联属性
     *
     * @param goodsAttrValues
     * @return
     */
    @Override
    public int add(GoodsAttrValues goodsAttrValues) {
        return mapper.add(goodsAttrValues);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public GoodsAttrValues detail(Integer id) {
        return mapper.detail(id);
    }

    /**
     * 更新
     *
     * @param goodsAttrValues
     * @return
     */
    @Override
    public int update(GoodsAttrValues goodsAttrValues) {
        return mapper.update(goodsAttrValues);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer... id) {
        if(id.length==1){
            return mapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
            count += mapper.delete(integer);
        }
        return count;
    }

    /**
     * 获取全部的商品关联属性
     *
     * @return
     */
    @Override
    public List<GoodsAttrValues> getAll(Integer goodsId) {
        return mapper.getAll(goodsId);
    }
}
